package com.battcn.platform.controller.money;


import com.battcn.framework.common.annotation.BattcnLog;
import com.battcn.framework.common.exception.BattcnException;
import com.battcn.framework.mybatis.page.DataGrid;
import com.battcn.platform.controller.BaseController;
import com.battcn.platform.pojo.dto.money.ExpenditureDto;
import com.battcn.platform.pojo.dto.money.ExpenditureRecordDto;
import com.battcn.platform.pojo.message.ApiResult;
import com.battcn.platform.pojo.po.money.TbExpenditure;
import com.battcn.platform.pojo.po.money.TbExpenditureRecord;
import com.battcn.platform.service.money.ExpenditureService;
import com.battcn.platform.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

import static com.battcn.framework.common.exception.BattcnException.notFound;

@Controller
@RequestMapping("/money/audit")//此处填写t_sys_menu的channel
@ApiIgnore//不加入swagger的扫描中去
public class AuditController extends BaseController{
	
	@Autowired
	ExpenditureService expenditureService;

	@RequestMapping(value = "/list")
	public String list() throws BattcnException {
		return "money/expenditure/auditList";
	}

	@RequestMapping(value = "/query")
	@ResponseBody
	public PageInfo<ExpenditureDto> query(DataGrid grid, ExpenditureDto dto) {
		if(StringUtils.isEmpty(dto.getStatus())){
            dto.setStatus(null);
		}else{
		    if(dto.getStatus().equals("1")){
                dto.setRecordId(SessionUtil.getSession().getId());//我审批过的列表
            }
        }
        dto.setUser(SessionUtil.getSession().getId());//查找我负责的部门下的申请列表

		return expenditureService.findRecordPage(grid,dto);
	}

	@GetMapping(value = "/edit")
	public String edit(ExpenditureRecordDto dto) {
		if (StringUtils.isNotEmpty(dto.getExpid())) {
			List<ExpenditureRecordDto> dtos = this.expenditureService.findRecordList(dto);
			request.setAttribute("records", dtos);
			request.setAttribute("dto", this.expenditureService.selectById(dto.getExpid()).orElseThrow(() -> notFound("未找到记录")));
		}
		return "money/expenditure/auditEdit";
	}

	@BattcnLog(module = "操作管理", methods = "保存操作", description = "添加经费申请信息")
	@PostMapping(value = "/save")
	@ResponseBody
	public ApiResult<String> save(TbExpenditure expenditure) {
		expenditure.setStatus("0");//未审核
		return ApiResult.getResponse(this.expenditureService.insertSelective(expenditure));
	}

	@BattcnLog(module = "删除管理",methods = "移除操作",description = "删除申请经费")
	@PostMapping(value = "remove")
	@ResponseBody
	public ApiResult<String> remove(String[] ids){
		Lists.newArrayList(ids).forEach(this.expenditureService::deleteById);
		return ApiResult.SUCCESS;
	}
	@BattcnLog(module = "修改管理", methods = "保存操作", description = "审核申请经费")
	@PostMapping(value = "/updateStatus")
	@ResponseBody
	public ApiResult<String> updateStatus(TbExpenditureRecord record) {
		return expenditureService.updateStatus(record);
	}


}
